package hydrap2p.helper;

import java.io.FileNotFoundException;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TTransportException;

import hydrap2p.ServiceLocation;
import hydrap2p.download.Downloader;
import hydrap2p.library.Library;
import hydrap2p.logging.Logging;
import hydrap2p.modulemanager.ModuleManager;
import hydrap2p.servicemanager.ServiceManager;
import hydrap2p.search.Searcher;
import hydrap2p.speedlimiter.SpeedLimiter;
import hydrap2p.speedlistener.SpeedListener;

public class ServiceConnector {
	
	/**
	 * Will return a Manager.Client object, or null
	 * @param blars
	 * @return
	 * @throws TTransportException
	 */
	public static ServiceManager.Client getServiceManager()
	{
		try {
			return new ServiceManager.Client(new TPooledProtocol(new ServiceLocation("localhost",Utility.getManagerPort())));
		} catch (TTransportException e) {
			// Random error while fetching the object
		} catch (NumberFormatException e) {
			// Error parsing the port
		} catch (FileNotFoundException e) {
			// Error reading the settings file
		}
		return null;
	}
	
	public static ServiceLocation getLocation(String servicename)
	{
		ServiceManager.Client manager=getServiceManager();
		if (manager==null)
			return null;
		
		ServiceLocation location=null;
		try {
			location=manager.getServiceLocation(servicename);
		} catch (TException e1) {
			location=null;
		}
		return location;
	}
	
	public static <T> T getService(Class<T> clienttype, ServiceLocation location)
	{
		if (location==null)
			return null;
		
		try {
			return clienttype.getConstructor(TProtocol.class).newInstance(new TPooledProtocol(location));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static <T> T getService(Class<T> clienttype, String servicename)
	{
		ServiceLocation location=getLocation(servicename);
		if (location==null)
			return null;
		
		try {
			return clienttype.getConstructor(TProtocol.class).newInstance(new TPooledProtocol(location));
		} catch (Exception e) {
			return null;
		}
	}
	
	public static ModuleManager.Client getModuleManager()
	{
		return getService(ModuleManager.Client.class, "ModuleManager");
	}
	
	public static Logging.Client getLogger()
	{
		return getService(Logging.Client.class, "Logging");
	}
	
	public static Library.Client getLibrary()
	{
		return getService(Library.Client.class, "Library");
	}
	
	public static Downloader.Client getDownloader()
	{
		return getService(Downloader.Client.class, "Downloader");
	}
	
	public static Searcher.Client getSearcher()
	{
		return getService(Searcher.Client.class, "Searcher");
	}
	
	public static SpeedLimiter.Client getSpeedLimiter()
	{
		return getService(SpeedLimiter.Client.class, "SpeedLimiter");
	}
	
	public static SpeedListener.Client getSpeedListener()
	{
		return getService(SpeedListener.Client.class, "SpeedListener");
	}
	
	public static TProtocol getPooledConnection(ServiceLocation location)
	{
		try {
			return new TPooledProtocol(location);
		} catch (Exception e)
		{
			return null;
		}
	}
}
